package fs

import (
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"strings"
)

func ReadFile(filePath string) string {
	contents, err := ioutil.ReadFile(filePath)
	if err == nil {
		// 因为contents是[]byte类型，直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符
		result := strings.Replace(string(contents), "\n", "", 1)
		return result
	} else {
		log.Println("ReadFile:", err)
		return ""
	}
}

func GetFullPath(path string) string {
	file, _ := exec.LookPath(os.Args[0])
	path1, _ := filepath.Abs(file)
	index := strings.LastIndex(path1, string(os.PathSeparator))
	return filepath.Join(path1[:index], path)
}

func GetCurrentPath() string {
	_, filename, _, _ := runtime.Caller(1)

	return path.Dir(filename)
}
func PathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}
